package fgame.action
{
	import fgame.GameState;
	import fgame.unit.Actor3D;
	
	import flare.collisions.CollisionInfo;
	import flare.collisions.RayCollision;
	import flare.core.Pivot3D;
	
	import flash.geom.Vector3D;
	
	import ricky.game.Actor;
	import ricky.game.action.Act;
	import ricky.utils.TimeServer;
	
	public class Act3D extends Act
	{
		public function Act3D(actor:Actor3D=null)
		{
			if(actor)
				this.actor=actor;
		}

		public function set rayCollisions(value:RayCollision):void
		{
			_rayCollisions = value;
		}

		/**  射线检测 */
		public function get rayCollisions():RayCollision
		{
//			_rayCollisions=GameState.inst.sceneManager.rayCollision;
			return _rayCollisions;
		}

		/**
		 * 开始行动初始化
		 */
		override public function begin():void
		{
			endTime=-1;
			_beginTime=TimeServer.now();
			super.addLogic();
		}
		
		override public function end():void
		{
			super.removeLogic();
		}
		
		override public function action(time:Number):void
		{
			
		}
		
		/**
		 * 当前行动类型
		 * 主要用于动画播放
		 */
		public function get animProperty():String
		{
			return _animProperty;
		}
		
		/**
		 * @private
		 */
		public function set animProperty(value:String):void
		{
			_animProperty = value;
		}
		
		/**
		 * @private
		 */
		override public function set actor(value:Actor):void
		{
			_actor = value;
			if(_actor)
			{
				this._currActor=_actor as Actor3D;
				_graphic=_actor.graphic as Pivot3D;
			}else
			{
				_currActor=null;
				_graphic=null;
			}
			
		}
		
		override public function dispose():void
		{
			super.removeLogic();
			actor=null;
			super.dispose();
			_completeFun=null;
		}
		
		public function get currActor():Actor3D
		{
			return _currActor
		}
		
		
		public function get graphic():Pivot3D
		{
			return _graphic;
		}
		
		//
		/**
		 * 让物体站立在地毯层上
		 */
		protected function setToSurface():void
		{
			if(test>6)
			{
				var from:Vector3D = graphic.localToGlobal( new Vector3D( 0, 50, 0 ) );
				var dir:Vector3D = graphic.getDown();
				_rayCollisions=GameState.inst.sceneManager.rayCollision;
				if(rayCollisions.test(from,dir))
				{
					var info:CollisionInfo = rayCollisions.data[0];
					graphic.setPosition( info.point.x, info.point.y, info.point.z );
					graphic.setNormalOrientation( info.normal, 0.05 );
				}
				test=0;
			}
			
			test++;
		}
		
		private var test:int=0;
		//
		protected var _animProperty:String;
		/** 3d行动对象 */
		protected var _currActor:Actor3D;
		/**  graphic **/
		protected var _graphic:Pivot3D;
		protected var _rayCollisions:RayCollision;
		/** 结束时间  */
		protected var endTime:Number=0
		/** 开始时间  */	
		protected var _beginTime:Number;
		

	}
}